package com.bw.repair.page

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.path.RouterPath
import com.bw.common.event.CustomEvent
import com.bw.common.view.BaseMvvmActivity
import com.bw.repair.R
import com.bw.repair.adapter.RepairImageAdapter
import com.bw.repair.databinding.ActivityAddRepairBinding
import com.bw.repair.entity.AddRepairEntity
import com.bw.repair.vm.RepairViewModel
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.EventBus
import java.text.SimpleDateFormat
import kotlin.random.Random

@Route(path = RouterPath.PATH_ADD_REPAIR_PATH)
@AndroidEntryPoint
class AddRepairActivity : BaseMvvmActivity<ActivityAddRepairBinding>() {

    private var addNumber = Random.nextInt(900000000)+1000000
    private var addTime = ""
    private var months = ""
    private var days = ""
    private val mAdapter = RepairImageAdapter()
    override fun getLayoutId(): Int =  R.layout.activity_add_repair
    private val mViewModel:RepairViewModel by viewModels()
    override fun initViews() {
        super.initViews()

        //选择时间
        mDataBinding.addRepairTime.setOnClickListener {
            val dataPicker = DatePicker(this)
            dataPicker.setTitle("选择日期")
            dataPicker.wheelLayout.setRange(
                DateEntity.target(2000, 1, 1),
                DateEntity.target(2049, 12, 1)
            )
            dataPicker.setOnDatePickedListener { year, month, day ->
                if(month>=10){
                    months="${month}"
                }else{
                    months="0${month}"
                }
                if(day>=10){
                    days="$day"
                }else{
                    days="0$day"
                }
                val simpleDateFormat = SimpleDateFormat("HH:mm")

                addTime = Editable.Factory.getInstance().newEditable("${year}-${months}-${days} "+simpleDateFormat.format(System.currentTimeMillis()))
                    .toString()
                mDataBinding.addRepairTime.text = addTime
            }
            dataPicker.show()
        }
        //选择照片
        //点击添加图片
        mDataBinding.addImage.setOnClickListener {
            if (mAdapter.data.size<3) {
                PictureSelector.create( this,200).selectPicture()
            }else{
                ToastUtils.showLong("最多添加4张图片")
            }
        }
        mDataBinding.addRepairRvImg.layoutManager= GridLayoutManager(this,3)
        mDataBinding.addRepairRvImg.adapter=mAdapter
        mDataBinding.addRepairRvImg.visibility= View.VISIBLE


        mDataBinding.btnSubmit.setOnClickListener {
            if (mAdapter.data.size<1){
                ToastUtils.showLong("请添加图片")
                return@setOnClickListener
            }
            val addRepair = AddRepairEntity(
                contactMethod =  mDataBinding.addRepairPhone.text.toString(),
                faultDescription = mDataBinding.addRepairType.text.toString(),
                faultPhotos = "",
                id = 0,
                orderNumber ="WX${addNumber}",
                processingStatus = "待处理",
                repairLocation = mDataBinding.addRepairArea.text.toString(),
                repairName = mDataBinding.addRepairName.text.toString(),
                repairTime =addTime,
                userId = 1
            )
            mViewModel.getAddRepairData(addRepair)
        }


    }



    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 200 && resultCode == RESULT_OK) {
            val bean =
                data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            mAdapter.addData(bean?.path!!)
            mAdapter.notifyDataSetChanged()
        }
    }

    //观察数据变化
    override fun observeDataChanged() {
        super.observeDataChanged()
        mViewModel.mAddRepairSuccess.observe(this){
            EventBus.getDefault().post(
                CustomEvent(RouterPath.PATH_REPAIR_PATH,1,1)
            )
            ToastUtils.showLong("报修提交成功")
            finish()
        }
    }

}